package com.vitas.coin.ui.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import java.util.List;

public class ViewPageAdapter extends FragmentStateAdapter {
    private List<Fragment> fragments;

    /**
     * 构造方法
     * @param fragment
     * @param fragments
     */
    public ViewPageAdapter(FragmentActivity fragment, List<Fragment> fragments) {
        super(fragment);
        this.fragments = fragments;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragments.get(position);
    }

    @Override
    public int getItemCount() {
        return fragments.size();
    }

    /**
     * 设置fragments并刷新
     * @param fragments
     */
    public void setFragments(List<Fragment> fragments) {
        this.fragments = fragments;
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {
        if (fragments.size() == 0 || fragments.size() <= position) {
            return 0;
        }
        return fragments.get(position).hashCode();
    }

    @Override
    public boolean containsItem(long itemId) {
        long position = 0;
        if (fragments.size() == 0 || fragments.size() <= itemId) {
        }else {
            position = fragments.get((int)itemId).hashCode();
        }
        return super.containsItem(position);
    }
}